package com.wss.module.user.ui.main.fragment;


import com.wss.common.base.BaseApplication;
import com.wss.common.base.BaseMvpFragment;
import com.wss.common.bean.Event;
import com.wss.common.bean.User;
import com.wss.common.constants.EventAction;
import com.wss.common.manage.AbilityToAbility;
import com.wss.common.utils.ImageUtils;
import com.wss.common.widget.MultipleItemView;
import com.wss.common.widget.dialog.AppDialog;
import com.wss.module.user.ResourceTable;
import com.wss.module.user.ui.about.AboutActivity;
import com.wss.module.user.ui.account.LoginActivity;
import com.wss.module.user.ui.main.mvp.UserPresenter;
import com.wss.module.user.ui.main.mvp.contract.UserContract;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.Lifecycle;
import ohos.agp.components.*;
import ohos.app.Context;

/**
 * Describe：个人中心
 * Created by 吴天强 on 2018/10/17.
 */

public class UserFragment extends BaseMvpFragment<UserPresenter>
        implements UserContract.View,
        Component.ClickedListener {

    private static final String URL = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1597207810717&di=4498f60a4b64fb7a436943bb420c5e1e&imgtype=0&src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F5bb3acc1916253d3228ab37879d910bfcbe1a7ce1df00a-ld0gg1_fw658";


    Image ivBg;
    Image ivHead;
    Text tvLoginOut;
    DirectionalLayout llLogged;
    Text tvLogin;
    Text tvName;
    Text tvEmail;

    private MultipleItemView mivAbout;
    private MultipleItemView mivCheck;

    public UserFragment(Ability abilitySlice) {
        super(abilitySlice);
    }

    @Override
    public void onViewCreated(Component view) {
        super.onViewCreated(view);
        init();
    }

    @Override
    protected int getLayoutId() {
        return ResourceTable.Layout_user_layout_user;
    }

    @Override
    protected void initView() {

        findView();

        ImageUtils.loadImageBlur(ivBg, URL);
        ImageUtils.loadImageCircle(ivHead, URL);
        refreshPage();
    }

    private void findView() {
        ivBg = (Image) findComponentById(ResourceTable.Id_iv_bg);
        ivHead = (Image) findComponentById(ResourceTable.Id_iv_head);
        tvLoginOut = (Text) findComponentById(ResourceTable.Id_tv_login_out);
        llLogged = (DirectionalLayout) findComponentById(ResourceTable.Id_ll_logged);
        tvLogin = (Text) findComponentById(ResourceTable.Id_tv_login);
        tvName = (Text) findComponentById(ResourceTable.Id_tv_name);
        tvEmail = (Text) findComponentById(ResourceTable.Id_tv_email);

        mivAbout = (MultipleItemView) findComponentById(ResourceTable.Id_miv_about);
        mivCheck = (MultipleItemView) findComponentById(ResourceTable.Id_miv_check);

        tvLogin.setClickedListener(this);
        mivAbout.setClickedListener(this);
        mivCheck.setClickedListener(this);
        tvLoginOut.setClickedListener(this);
    }

    private void refreshPage() {
        if (BaseApplication.i().isLogged()) {
            User user = BaseApplication.i().getUser();
            System.out.println("==== get user : " + user.toString());
            llLogged.setVisibility(Component.VISIBLE);
            tvLoginOut.setVisibility(Component.VISIBLE);
            tvLogin.setVisibility(Component.HIDE);
            tvName.setText(user.getUsername());
            tvEmail.setText(user.getEmail()==null||"".equals(user.getEmail()) ? "暂无邮箱" : user.getEmail());
        } else {
            llLogged.setVisibility(Component.HIDE);
            tvLogin.setVisibility(Component.VISIBLE);
            tvLoginOut.setVisibility(Component.HIDE);
        }
    }

    /**
     * 检查登录
     *
     * @return boolean
     */
    private boolean checkLogin() {
        if (BaseApplication.i().isLogged()) {
            return true;
        }
        new AppDialog.Builder(context)
                .setContent("请您先登录")
                .setRightButton(val -> AbilityToAbility.toAbility(context, "LoginActivity"))
                .create()
                .show();
        return false;
    }

    @Override
    public void onEventBus(Event event) {
        super.onEventBus(event);

        if (EventAction.EVENT_LOGIN_SUCCESS.equals(event.getAction()) ||
                EventAction.EVENT_REGISTER_SUCCESS.equals(event.getAction())) {
            //登录成功、注册成功刷新页面
            refreshPage();
        }
    }

    @Override
    protected boolean registerEventBus() {
        return true;
    }

    @Override
    protected UserPresenter createPresenter() {
        return new UserPresenter();
    }

    @Override
    public Context getContext() {
        return getContext();
    }

//    @Override
//    public Lifecycle getLifecycle() {
//        return null;
//    }

    @Override
    public void onClick(Component component) {
        int i = component.getId();
        if (i == ResourceTable.Id_tv_login) {
            //去登录
            AbilityToAbility.toAbility(context, LoginActivity.class);
        } else if (i == ResourceTable.Id_miv_collection) {
            //我的收藏
        } else if (i == ResourceTable.Id_miv_order) {
            //我的订单
        } else if (i == ResourceTable.Id_miv_about) {
            //关于
            AbilityToAbility.toAbility(context, AboutActivity.class);
        } else if (i == ResourceTable.Id_miv_check) {
            getPresenter().checkUpdate();
        } else if (i == ResourceTable.Id_tv_login_out) {
            new AppDialog.Builder(context)
                    .setContent("是否确定退出？")
                    .setRightButton(val -> {
                        BaseApplication.i().setUser(null);
                        refreshPage();
                    })
                    .create()
                    .show();
        }
    }
}
